import { reactive, toRaw } from 'vue';
import { CascaderOption } from 'vant/lib/cascader/types';
import type { SeriesItem } from '/@/api/verse/model/seriesModel';
import { RarityEnum } from '/@/api/verse/model/cardModel';
import { SeriesListApi } from '/@/api/verse/series';
import { uniqBy, filter } from 'lodash-es';

const rarityOptions: CascaderOption[] = [
  { label: RarityEnum.BRONZE, value: RarityEnum.BRONZE },
  { label: RarityEnum.SILVER, value: RarityEnum.SILVER },
  { label: RarityEnum.GOLD, value: RarityEnum.GOLD },
  { label: RarityEnum.LEGEND, value: RarityEnum.LEGEND },
  { label: RarityEnum.LD, value: RarityEnum.LD },
  { label: RarityEnum.UR, value: RarityEnum.UR },
  { label: RarityEnum.SL, value: RarityEnum.SL },
  { label: RarityEnum.TOKEN, value: RarityEnum.TOKEN },
];

const careerOptions: CascaderOption[] = [
  { label: '中立', value: '中立' },
  { label: '精灵', value: '精灵' },
  { label: '皇家', value: '皇家' },
  { label: '巫师', value: '巫师' },
  { label: '龙族', value: '龙族' },
  { label: '梦魇', value: '梦魇' },
  { label: '主教', value: '主教' },
];

const categoryOptions: CascaderOption[] = [
  { label: '随从', value: '随从' },
  { label: '法术', value: '法术' },
  { label: '护符', value: '护符' },
  { label: '主战者', value: '主战者' },
];

const manaOptions: CascaderOption[] = [
  { label: '0', value: 0 },
  { label: '1', value: 1 },
  { label: '2', value: 2 },
  { label: '3', value: 3 },
  { label: '4', value: 4 },
  { label: '5', value: 5 },
  { label: '6', value: 6 },
  { label: '7', value: 7 },
  { label: '8+', value: -2 },
];

const seriesOptions = () => {
  type YearSeries = {
    year: number;
    list: SeriesItem[];
  };
  const seriesData = reactive<SeriesItem[]>([]);
  const yearSeriesData = reactive<YearSeries[]>([]);
  const seriesAll = () => {
    SeriesListApi()
      .then((result) => {
        for (const item of result) {
          seriesData.push(item);
        }
        handleSeriesDate(toRaw(seriesData));
      })
      .catch(() => {});
  };
  seriesAll();
  const handleSeriesDate = (data: SeriesItem[]) => {
    yearSeriesData.length = 0;
    const yearArr = uniqBy(toRaw(data), 'publish_year');
    for (const year of yearArr) {
      const list = filter(toRaw(data), (item: SeriesItem) => {
        return year.publish_year === item.publish_year;
      });
      yearSeriesData.push({ year: year.publish_year, list: list });
    }
  };
  return {
    seriesData,
    yearSeriesData,
    handleSeriesDate,
  };
};

export function useOptions() {
  return {
    rarityOptions,
    careerOptions,
    categoryOptions,
    manaOptions,
    seriesOptions,
  };
}
